import sun.management.ThreadInfoCompositeData;

import java.util.Comparator;
import java.util.FormatFlagsConversionMismatchException;

public class Test {
    public static void main(String[] args) {
        Person person1=new Person("a",15);
        Person person2=new Person("b",12);
        Person person3=new Person("c",17);
        Person person4=new Person("d",13);
        Person person5=new Person("e",19);//实例化五个对象
        Person[] people={person1,person5,person3,person2,person4};//乱序
        Sort.sort(people);//这个时候就排好了
        System.out.println(people[0]);
        System.out.println(people[1]);
        System.out.println(people[2]);
        System.out.println(people[3]);
        System.out.println(people[4]);

    }
}
class Person implements Comparable{
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public int compareTo(Object o) {
        return this.name.compareTo(((Person)o).name);
    }
}

class Sort {
    public static void sort(Comparable[] comparable) {
        //注意这里用接口数组表示实现了这个接口也就是这个数组一定是可比较的
        for (int i = 0; i < comparable.length - 1; i++) {
            for (int j = 0; j < comparable.length - 1; j++) {
                //标准冒泡排序
                if (comparable[j].compareTo(comparable[j + 1]) > 0) {
                    //因为比较的是任意类型数据，这里一定是调用我们重写的compareTo方法
                    Comparable tmp=comparable[j];
                    comparable[j]=comparable[j+1];
                    comparable[j+1]=tmp;
                }
            }
        }
    }
}